// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
//	The file contains global variables and settings for the WebExpense setting
//
//	Carolyn Van Slyck 06/2003 - Created
//  DaveMM - Updates 06/2005 - Tweaks, Updates, Fixes for SDK
//	Revision: v1.0

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using Microsoft.Interop.Security.AzRoles;
using System.Runtime.InteropServices;

//
//	Application Namespace
//
namespace WebExpense 
{
	/// <summary>
	/// Global Settings and Methods
	/// </summary>
	public class Global : System.Web.HttpApplication
	{
		/// <summary>
		/// Initializes application variables when the application
		/// is first created
		/// </summary>
		protected void Application_Start(Object sender, EventArgs e)
		{
			//
			//	Lock the application variables while editing
			//
			Application.Lock();

			//
			//	Stores the application name
			//
			Application["AZMAN_APP_NAME"] = "Expense Web";
			
			//
			//	Stores the Authorization Manager policy store object
			//
			Application["AZMAN_STORE"] = "AzManStore";

			//
			//	Stores the Authorization Manager application object
			//
			Application["AZMAN_APP"] = "AzManApp";

			//
			//	Stores the Authorization Manager policy store path
			//
			Application["STORE_PATH"]="msxml://c:\\inetpub\\wwwroot\\WebExpense\\AzStore.xml";
			
			//
			//	Uncomment the line below to use Active Directory for the policy store
			//	You will need to configure the connection string
			//
			//Application["STORE_PATH"]="msldap://CN=AzStore,CN=Program Data,DC=microsoft,DC=com";
			
			//
			//	Stores the maximum number of transactions before the demo resets itself
			//
			Application["DATASTORE_MAXTRANS"] = 10;

			//
			//	Stores the ID of the last transaction to be created
			//
			Application["DATASTORE_LASTTRANS"]=0;

			//
			//	Stores the value of the Self Approval setting
			//	True - Approvers can approve their own expenses
			//	False - Approvers cannot approve their own expenses
			//
			Application["SELF_APPROVAL"]=false;
			
			//
			//	Unlock the application variables
			//
			Application.UnLock();

            ExpenseCommon.Initialize();
		}
 
		/// <summary>
		/// Initializes all session variables when a user first connects
		/// to the application
		/// </summary>
		protected void Session_Start(Object sender, EventArgs e)
		{
			//
			//	Get the SAM name (\\domain\user) of the current user
			//	and store it in a session variable
			//
			NameValueCollection ServerVars;
			ServerVars = Request.ServerVariables;
			Session["CLIENT_SAM_NAME"] = ServerVars["LOGON_USER"];

			IAzClientContext AzManClientContext;

			//
			//	Create the client context from the user's token
			//
			//	ClientContext can be initialized from either a user's account name (InitializeClientContextFromName), 
			//	a user's token (InitializeClientContextFromToken) or from a string SID (InitializeClientContextFromStringSid)
			//
			HandleRef token = new HandleRef(new object(), ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetUserToken());
			AzManClientContext = ((IAzApplication)Application["AZMAN_APP"]).InitializeClientContextFromToken((UInt64)token.Handle, 0);

			//
			//	Save the client context in a session variable
			//
			Session["AZMAN_CLIENT"] = AzManClientContext;
		}
			
		#region Web Form Designer generated code	

		/// <summary>
		/// Global Contructor - Auto generated by Visual Studio
		/// </summary>	
		public Global()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Required designer variable.
		/// </summary>
		///
		//private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			//this.components = new System.ComponentModel.Container();
		}
		#endregion
	}
}